<?php
/**
 * Created by PhpStorm.
 * Author: netman
 * QQ: 1649513971
 * Authorize:http://www.stourweb.com
 * Time: 2018/8/2 9:06
 * Desc:
 */
defined('SYSPATH') or die('No direct access allowed.');
class Model_Zhilvyun_Api_Stapi {


    private $_app_id = 'xyy2019011205353110';
    private $_app_secret = 'YBeWZr7p2V8KwQKq24nXTlPcJpkKwZxR';
    private $_api_url = 'http://www.v6.com/distribute/zhilvyun/api';
    private $_sign = '';

    public function __construct($app_id='',$app_secret='',$api_url='')
    {
        $this->_app_id = $app_id ? $app_id : $this->_app_id;
        $this->_app_secret = $app_secret ? $app_secret :$this->_app_secret;
        $this->_api_url = $api_url ? $api_url : $this->_api_url;
    }

    /**
     * 验证签名
     * @param Request $request
     * @param string $connection
     * @return bool
     */
    public  function check_signature($params)
    {

        //验证签名
        $pre_string = $this->create_link_string($params);
        $now_signature = $this->md5_sign($pre_string,$this->_app_secret);

        if($now_signature == $params['sign'])
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    /**
     * 生成签名.
     * @param $param_array 请求的参数数组
     * @return string
     */
    public function make_sign(&$param_array)
    {
        //加上随机时间
        $param_array['timestamp'] = time();
        $pre_string =$this->create_link_string($param_array);
        $sign = $this->md5_sign($pre_string,$this->_app_secret);
        $this->_sign = $sign;
        return $sign;


    }
    /**
     * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param $para 需要拼接的数组
     * return 拼接完成以后的字符串
     */
    public function create_link_string($para)
    {

        $para = $this->param_filter($para);
        $para = $this->param_sort($para);
        $arg  = "";

        foreach($para as $key => $val)
        {
            if(!is_array($val))
            {
                $arg.=$key."=".$val."&";
            }

        }
        //去掉最后一个&字符
        $arg = substr($arg,0,strlen($arg)-1);

        //如果存在转义字符，那么去掉转义
        if(get_magic_quotes_gpc())
        {
            $arg = stripslashes($arg);
        }

        return $arg;
    }

    /**
     * 生成签名
     * @param $prestr
     * @param $app_secret
     * @return string
     */
    public function md5_sign($prestr, $app_secret)
    {
        $prestr = $prestr . $app_secret;
        return md5($prestr);
    }


    /**
     * 除去数组中的空值和签名参数
     * @param $para 签名参数组
     * return 去掉空值与签名参数后的新签名参数组
     */
    public function param_filter($para)
    {
        $para_filter = array();
        foreach($para as $key=>$val)
        {
            if($key == "sign" || $key == "sign_type")continue;
            else	$para_filter[$key] = $para[$key];
        }

        return $para_filter;
    }
    /**
     * 对数组排序
     * @param $para 排序前的数组
     * return 排序后的数组
     */
    public function param_sort($para)
    {
        ksort($para);
        reset($para);
        return $para;
    }


    public function http($request_param)
    {
        $request_param['app_id'] = $this->_app_id;
        $request_param['domain'] = $this->get_domain();
        $this->make_sign($request_param);
        if(!$this->_sign)
        {
            exit('the sign is not make,please make it first');
        }
        $uri = $this->_api_url.$request_param['method'];



        $request_param['sign'] = $this->_sign;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_param));
        $rtn = curl_exec($ch);
        curl_close($ch);
        if($this->is_json($rtn))
        {
            return json_decode($rtn,true);
        }
        else
        {
            return $rtn;
        }

    }
    //判断是否为json
    private function is_json($string)
    {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }

    public function get_domain()
    {
        $host = parse_url(URL::base(true));
        $domain = sprintf("%s://%s/", $host['scheme'], $host['host']);
        return $domain;
    }

}